package com.ruoyi.web.controller.system;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import com.ruoyi.ucar.common.CommonVoResp;
import com.ruoyi.ucar.util.CommonUtil;
import com.ruoyi.ucar.voreq.DictDataListVoReq;
import com.ruoyi.ucar.voreq.DictDataVoReq;
import com.ruoyi.ucar.voresp.DictDataDetailVo;
import com.ruoyi.ucar.voresp.DictDataListVo;
import com.ruoyi.ucar.voresp.DictDataListVoResp;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/ucar/dict")
@Api(value = "字典管理",tags = "字典管理模块接口")
public class DictDataController extends BaseController {

    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private ISysDictDataService dictDataService;
    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation("字典类型查询所有字典数据信息")
    @PostMapping(value = "/list")
    public CommonVoResp<DictDataListVoResp> dictType(@RequestBody DictDataListVoReq dictDataListVoReq)
    {
        CommonVoResp<DictDataListVoResp> commonVoResp=new CommonVoResp<>();
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictDataListVoReq.getDictType());
        if (StringUtils.isNotNull(data)&&data.size()>0)
        { DictDataListVoResp dictDataListVoResp=new DictDataListVoResp();
            List<DictDataListVo> dictDataListVos=new ArrayList<>();
            data.forEach(sysDictData -> {
                DictDataListVo dictDataListVo=CommonUtil.objToObj(sysDictData,DictDataListVo.class);
                dictDataListVos.add(dictDataListVo);
            });
            dictDataListVoResp.setDictDataListVos(dictDataListVos);
            commonVoResp.setData(dictDataListVoResp);
        }else{
            return CommonUtil.setCommonVoRespNoDataError(commonVoResp);
        }
        return commonVoResp;
    }


     /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation("字典类型和字典项键值查询单个字典详细信息")
    @PostMapping(value = "/dictData")
    public CommonVoResp<DictDataDetailVo> dictData(@RequestBody DictDataVoReq dictDataVoReq)
    {
        CommonVoResp<DictDataDetailVo> commonVoResp=new CommonVoResp<>();
        SysDictData sysDictData = dictDataService.selectDictByDictValue(dictDataVoReq.getDictType(),dictDataVoReq.getDictValue());
        if (StringUtils.isNotNull(sysDictData) )
        {
            DictDataDetailVo dictDataDetailVo=CommonUtil.objToObj(sysDictData,DictDataDetailVo.class);
            commonVoResp.setData(dictDataDetailVo);
        }else{
            return CommonUtil.setCommonVoRespNoDataError(commonVoResp);
        }
        return commonVoResp;
    }


}
